home *** CD-ROM | disk | FTP | other *** search
/ PC Users 1998 June / Cd Pc Users 9.iso / prog / inst / baslibs / basmisc.bas < prev    next >
Encoding:
BASIC Source File  |  1996-12-18  |  2.7 KB  |  122 lines

  1. Attribute VB_Name = "basMisc"
  2. Option Explicit
  3. ' Misc. functions.
  4.  
  5. '
  6. '  Sets a given bit in num
  7. '
  8. Public Function SetBit(Num As Long, ByVal bit As Long) As Long
  9.    If bit = 31 Then
  10.       Num = &H80000000 Or Num
  11.    Else
  12.       Num = (2 ^ bit) Or Num
  13.    End If
  14.    
  15.    SetBit = Num
  16. End Function
  17.  
  18. '
  19. '  clears a given bit in num
  20. '
  21. Public Function ClearBit(Num As Long, ByVal bit As Long) As Long
  22.    If bit = 31 Then
  23.       Num = &H7FFFFFFF And Num
  24.    Else
  25.       Num = ((2 ^ bit) Xor &HFFFFFFFF) And Num
  26.    End If
  27.    
  28.    ClearBit = Num
  29. End Function
  30.  
  31. '
  32. '  Test if bit 0 to bit 31 is set.
  33. '
  34. Public Function IsBitSet(ByVal Num As Long, ByVal bit As Long) As Boolean
  35.    IsBitSet = False
  36.    
  37.    If bit = 31 Then
  38.       If Num And &H80000000 Then
  39.          IsBitSet = True
  40.       End If
  41.    Else
  42.       If Num And (2 ^ bit) Then
  43.          IsBitSet = True
  44.       End If
  45.    End If
  46. End Function
  47.  
  48. ' Centers a form relative to the screen or
  49. ' another form
  50. Public Sub CenterForm(f As Form, Optional f2 As Variant)
  51.    If IsMissing(f2) Then
  52.       f.Move (Screen.Width - f.Width) / 2, _
  53.              (Screen.Height - f.Height) / 2
  54.    Else
  55.       ' If f is an MDI child in a MDI parent then
  56.       ' center f within the parent.
  57.       If f.MDIChild And Not f2.MDIChild Then
  58.             f.Move ((f2.Width - f.Width) / 2), _
  59.             ((f2.Height - f.Height) / 2)
  60.       Else
  61.          f.Move ((f2.Width - f.Width) / 2) + f2.Left, _
  62.                 ((f2.Height - f.Height) / 2) + f2.Top
  63.       End If
  64.    End If
  65. End Sub
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. Public Function IsEven(ByVal i As Long) As Boolean
  74.    IsEven = Not -(i And 1)
  75. End Function
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. '
  83. '  Returns true if the year is a leap year.
  84. '     yr is either a date or an integer
  85. '
  86. Public Function IsLeapYear(yr As Variant) As Boolean
  87.    If VarType(yr) = vbDate Then
  88.       IsLeapYear = (Day(DateSerial(Year(yr), 2, 29)) = 29)
  89.    Else
  90.       IsLeapYear = (Day(DateSerial(yr, 2, 29)) = 29)
  91.    End If
  92. End Function
  93.  
  94. Public Function IsOdd(ByVal i As Long) As Boolean
  95.    IsOdd = -(i And 1)
  96. End Function
  97.  
  98. '
  99. '  Scrambles the order of elements in an array.
  100. '
  101. Public Sub ShuffleArray(ByRef vArray As Variant, Optional startIndex As Variant, Optional endIndex As Variant)
  102.     Dim i As Long
  103.     Dim rndIndex As Long
  104.     Dim Temp As Variant
  105.     
  106.     If IsMissing(startIndex) Then
  107.        startIndex = LBound(vArray)
  108.     End If
  109.     
  110.     If IsMissing(endIndex) Then
  111.        endIndex = UBound(vArray)
  112.     End If
  113.  
  114.     For i = startIndex To endIndex
  115.         rndIndex = Int((endIndex - startIndex + 1) * Rnd() + startIndex)
  116.  
  117.         Temp = vArray(i)
  118.         vArray(i) = vArray(rndIndex)
  119.         vArray(rndIndex) = Temp
  120.     Next i
  121. End Sub
  122.